using ICSharpCode.TextEditor.Document;
using System;
using System.Drawing;
using System.Windows.Forms;
namespace ICSharpCode.TextEditor
{
	public class TextAreaDragDropHandler
	{
		public static Action<Exception> OnDragDropException = delegate(Exception ex)
		{
			MessageBox.Show(ex.ToString());
		}
		;
		private TextArea textArea;
		public void Attach(TextArea textArea)
		{
			this.textArea = textArea;
			textArea.AllowDrop = true;
			textArea.DragEnter += TextAreaDragDropHandler.MakeDragEventHandler(new DragEventHandler(this.OnDragEnter));
			textArea.DragDrop += TextAreaDragDropHandler.MakeDragEventHandler(new DragEventHandler(this.OnDragDrop));
			textArea.DragOver += TextAreaDragDropHandler.MakeDragEventHandler(new DragEventHandler(this.OnDragOver));
		}
		private static DragEventHandler MakeDragEventHandler(DragEventHandler h)
		{
			return delegate(object sender, DragEventArgs e)
			{
				try
				{
					h(sender, e);
				}
				catch (Exception obj)
				{
					TextAreaDragDropHandler.OnDragDropException(obj);
				}
			}
			;
		}
		private static DragDropEffects GetDragDropEffect(DragEventArgs e)
		{
			if ((e.AllowedEffect & DragDropEffects.Move) > DragDropEffects.None && (e.AllowedEffect & DragDropEffects.Copy) > DragDropEffects.None)
			{
				if ((e.KeyState & 8) <= 0)
				{
					return DragDropEffects.Move;
				}
				return DragDropEffects.Copy;
			}
			else
			{
				if ((e.AllowedEffect & DragDropEffects.Move) > DragDropEffects.None)
				{
					return DragDropEffects.Move;
				}
				if ((e.AllowedEffect & DragDropEffects.Copy) > DragDropEffects.None)
				{
					return DragDropEffects.Copy;
				}
				return DragDropEffects.None;
			}
		}
		protected void OnDragEnter(object sender, DragEventArgs e)
		{
			if (e.Data.GetDataPresent(typeof(string)))
			{
				e.Effect = TextAreaDragDropHandler.GetDragDropEffect(e);
			}
		}
		private void InsertString(int offset, string str)
		{
			this.textArea.Document.Insert(offset, str);
			this.textArea.SelectionManager.SetSelection(new DefaultSelection(this.textArea.Document, this.textArea.Document.OffsetToPosition(offset), this.textArea.Document.OffsetToPosition(offset + str.Length)));
			this.textArea.Caret.Position = this.textArea.Document.OffsetToPosition(offset + str.Length);
			this.textArea.Refresh();
		}
		protected void OnDragDrop(object sender, DragEventArgs e)
		{
			this.textArea.PointToClient(new Point(e.X, e.Y));
			if (e.Data.GetDataPresent(typeof(string)))
			{
				this.textArea.BeginUpdate();
				this.textArea.Document.UndoStack.StartUndoGroup();
				try
				{
					int num = this.textArea.Caret.Offset;
					if (!this.textArea.IsReadOnly(num))
					{
						if (e.Data.GetDataPresent(typeof(DefaultSelection)))
						{
							ISelection selection = (ISelection)e.Data.GetData(typeof(DefaultSelection));
							if (selection.ContainsPosition(this.textArea.Caret.Position))
							{
								return;
							}
							if (TextAreaDragDropHandler.GetDragDropEffect(e) == DragDropEffects.Move)
							{
								if (SelectionManager.SelectionIsReadOnly(this.textArea.Document, selection))
								{
									return;
								}
								int length = selection.Length;
								this.textArea.Document.Remove(selection.Offset, length);
								if (selection.Offset < num)
								{
									num -= length;
								}
							}
						}
						this.textArea.SelectionManager.ClearSelection();
						this.InsertString(num, (string)e.Data.GetData(typeof(string)));
						this.textArea.Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.WholeTextArea));
					}
				}
				finally
				{
					this.textArea.Document.UndoStack.EndUndoGroup();
					this.textArea.EndUpdate();
				}
			}
		}
		protected void OnDragOver(object sender, DragEventArgs e)
		{
			if (!this.textArea.Focused)
			{
				this.textArea.Focus();
			}
			Point point = this.textArea.PointToClient(new Point(e.X, e.Y));
			if (!this.textArea.TextView.DrawingPosition.Contains(point.X, point.Y))
			{
				e.Effect = DragDropEffects.None;
				return;
			}
			TextLocation logicalPosition = this.textArea.TextView.GetLogicalPosition(point.X - this.textArea.TextView.DrawingPosition.X, point.Y - this.textArea.TextView.DrawingPosition.Y);
			int line = Math.Min(this.textArea.Document.TotalNumberOfLines - 1, Math.Max(0, logicalPosition.Y));
			this.textArea.Caret.Position = new TextLocation(logicalPosition.X, line);
			this.textArea.SetDesiredColumn();
			if (e.Data.GetDataPresent(typeof(string)) && !this.textArea.IsReadOnly(this.textArea.Caret.Offset))
			{
				e.Effect = TextAreaDragDropHandler.GetDragDropEffect(e);
				return;
			}
			e.Effect = DragDropEffects.None;
		}
	}
}
